package dao;

import java.sql.*;
import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
	@Override
    public void Insert(User user) throws Exception{		//插入一个用户
        DBConnection con = null;
        PreparedStatement pstm = null;
        String sql = "insert into User(Id,Username,Password,Money,Telnum,Prize,Num) values(?, ?, ?, ?,?,?,?)";
        try
        {
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            pstm.setDouble(1, user.getId());
            pstm.setString(2, user.getUsername());
            pstm.setString(3, user.getPassword());
            pstm.setDouble(4, user.getMoney());
            pstm.setString(5, user.getTelNum());
            pstm.setInt(6, user.getPrize());
            pstm.setString(7, user.getNum());
            pstm.execute();
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("插入数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
    }
	@Override
    public void Delete(int id) throws Exception{		//删
        DBConnection con = null;
        PreparedStatement pstm = null;
        String sql = "delete from user where Id=?";
        try
        {
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            pstm.setInt(1, id);
            pstm.execute();
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("删除数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
    }
	@Override
    public void Update(User user) throws Exception{		//改
        DBConnection con = null;
        PreparedStatement pstm = null;
        String sql = "Update user set Username=?,Password=?,Money=?,Telnum=?,Prize=?,Num=? where id=?";
        try
        {
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            pstm.setString(1, user.getUsername());
            pstm.setString(2, user.getPassword());
            pstm.setDouble(3, user.getMoney());
            pstm.setString(4, user.getTelNum());
            pstm.setInt(5, user.getPrize());
            pstm.setString(6, user.getNum());
            pstm.setInt(7, user.getId());
            pstm.executeUpdate();
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("修改数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
    }
	@Override
    public User QuerryById(int id) throws Exception{	//查	
        DBConnection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from user where Id=?";
        try
        {
        	
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            pstm.setInt(1, id);
            rs = pstm.executeQuery();
            user = new User();
            while(rs.next())
            {
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setMoney(rs.getDouble(4));
                user.setTelNum(rs.getString(5));
                user.setPrize(rs.getInt(6));
                user.setNum(rs.getString(7));
            }
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("查询数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
        return user;
    }
	
    public User QuerryByUsername(String username) throws Exception{	//查	
        DBConnection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from user where Username=?";
        try
        {
        	
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            pstm.setString(1, username);
            rs = pstm.executeQuery();
            user = new User();
            while(rs.next())
            {
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setMoney(rs.getDouble(4));
                user.setTelNum(rs.getString(5));
                user.setPrize(rs.getInt(6));
                user.setNum(rs.getString(7));
            }
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("查询数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
        return user;
    }
	
    public boolean Login(String username,String password) throws Exception{	
        DBConnection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String username1;
        String password1;
        String sql = "select * from user";
        int flag = 0;
        try
        {
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            rs = pstm.executeQuery();
            while(rs.next())
            {
            	username1 = rs.getString("Username");
            	password1 = rs.getString("Password");
            	if(username1.equals(username) && password1.equals(password)) {
            		flag = 1;
            		break;
            	}
            }
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("修改数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
        if(flag == 1)
        	return true;
        else 
        	return false;
    }
	
    
    public void LotteryDraw(ArrayList<Integer> Num) throws Exception {
        DBConnection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String Num1;
    	User u=new User();
        String sql = "select * from user";
        try
        {
            con = new DBConnection();
            pstm = con.getConnection().prepareStatement(sql);
            rs = pstm.executeQuery();
            while(rs.next())
            {
            	int count=0;
            	Num1 = rs.getString("Num");
            	String hu[]=Num1.split(" ");
            	for(int i=0;i<hu.length-1;i++)
            	{
            		int aa=Integer.parseInt(hu[i]);
            		if(Num.contains(aa))
            			count++;
            	}
            	UserDao userDao = DaoFactory.getUserDaoInstance();
            	
            	
            	u=userDao.QuerryByUsername(rs.getString("Username"));
            	//System.out.print(rs.getString("Username") + " ");
            	u.setPrize(count);
            	//System.out.print(u.getPrize() + " ");
            	userDao.Update(u);
            	

            	
            	System.out.println(count);
            	
            }
            if (pstm != null)
            {
                pstm.close();
            }
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("修改数据操作异常");
        } finally
        {
            if (con != null)
            {
                con.close();
            }
        }
        
    }
    
}
